//
//  LoveCityViewController.m
//  cityStar
//
//  Created by joyban on 15-1-5.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "LoveCityViewController.h"
#import "UIColor+FlatUI.h"
#import "LoveCityCell.h"
#import "selectProvince.h"

#import "selectCityArrayModel.h"
#import "selectCityValueModel.h"

NSString *const LoveCityCellID = @"LoveCityCellID";


@interface LoveCityViewController ()
@property (weak, nonatomic) IBOutlet UITableView *loveCityTable;
@property (nonatomic,strong) NSMutableArray *cellArray;
@property (nonatomic,strong) NSMutableArray *NamelArray;
@property (nonatomic,strong) NSMutableArray *MycellArray;
@property (nonatomic,strong) NSMutableArray *IDcellArray;

@end

@implementation LoveCityViewController

@synthesize LoveCityArray,selectLoveCity,cellArray;


- (void)viewDidLoad {
    [super viewDidLoad];
    selectLoveCity = @"杭州";
    self.MycellArray = [[NSMutableArray alloc]init];
    cellArray  = [[NSMutableArray alloc]init];
//    LoveCityArray = [selectProvince shardProvinceInfo].ProvinceNameArray;
    LoveCityArray = [[NSMutableArray alloc]initWithObjects:@"杭州",@"大连",@"台州市",@"宁波",@"绍兴",@"温州",@"桂林",@"西安",@"无锡", nil];
    self.navigationItem.title = @"偏好城市";
    [self addBackButton];
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithTitle:@"完成" actionBlock:^{
        NSArray *arr = [[NSArray alloc]initWithObjects:weakSelf.NamelArray,weakSelf.IDcellArray, nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"showCityArray" object:arr];
        [weakSelf.navigationController popToRootViewControllerAnimated:YES];

    }];
    [self webRequest_GetCityData];

    [self setUpTableViews];
}

-(void)webRequest_GetCityData
{
    //    NSString *postUrl = [NSString stringWithFormat:@"%@",KInterface_CityList];
    [HttpRequestManager getMethod:KInterface_CityList parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        NSLog(@"%@",response);
        if (success) {
            self.cellArray = response[@"value"];
            [self.loveCityTable reloadData];
        }else{
            
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpTableViews
{
    [self.loveCityTable registerNib:[LoveCityCell nib] forCellReuseIdentifier:LoveCityCellID];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.cellArray.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
    {
        return 1;
    }else{
        selectCityArrayModel *model = [selectCityArrayModel objectFromDictionary:self.cellArray[section - 1]];
        return model.city.count;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 25;
    }else if(section == 1)
    {
        return 43;
    }else{
        return 18;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [LoveCityCell heightOfTableCell];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section <= 1)
    {
        UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)];
        UILabel * label = [[UILabel alloc] init] ;
        label.frame = CGRectMake(15, 0, 200, 25);
        label.backgroundColor = [UIColor clearColor];
        label.font=[UIFont boldSystemFontOfSize:13];
        if(section == 0)
        {
            label.text = @"当前定位城市";
        }else
        {
            label.text = @"选择所在城市(最多设置5个)";
            UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width, 18)];
            backView.backgroundColor = [UIColor colorFromHexCode:@"ffffff"];
            [sectionView addSubview:backView];
            UILabel * label2 = [[UILabel alloc] init];
            
            UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 17, tableView.bounds.size.width, 1)];
            [line setBackgroundColor:[UIColor colorFromHexCode:@"FEF3E2"]];
            [backView addSubview:line];
            
            label2.frame = CGRectMake(15, 0, 200, 18);
            label2.backgroundColor = [UIColor clearColor];
            label2.font=[UIFont boldSystemFontOfSize:11];
            label2.textColor = [UIColor colorFromHexCode:@"5F5B56"];
            label2.textAlignment = NSTextAlignmentLeft;
            selectCityArrayModel *model = [selectCityArrayModel objectFromDictionary:self.cellArray[section - 1]];
            label2.text = model.first_letter;
            [backView addSubview:label2];
        }
        label.textColor = [UIColor colorFromHexCode:@"5F5B56"];
        label.textAlignment = NSTextAlignmentLeft;
        [sectionView addSubview:label];
        [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"FEF3E2"]];
        return sectionView;
    }else{
        UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)];
        [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"ffffff"]];
        UILabel * label2 = [[UILabel alloc] init] ;
        label2.frame = CGRectMake(15, 0, 200, 18);
        label2.backgroundColor = [UIColor clearColor];
        label2.font=[UIFont boldSystemFontOfSize:11];
        label2.textColor = [UIColor colorFromHexCode:@"5F5B56"];
        label2.textAlignment = NSTextAlignmentLeft;
        UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 17, tableView.bounds.size.width, 1)];
        [line setBackgroundColor:[UIColor colorFromHexCode:@"FEF3E2"]];
        [sectionView addSubview:line];
        selectCityArrayModel *model = [selectCityArrayModel objectFromDictionary:self.cellArray[section - 1]];
        label2.text = model.first_letter;
        [sectionView addSubview:label2];
        return sectionView;
    }

}

#pragma mark - UITableViewDelegate and UITableViewDataSource

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LoveCityCell *lovecityCell = [tableView dequeueReusableCellWithIdentifier:LoveCityCellID];
    lovecityCell.selectionStyle = UITableViewCellSelectionStyleNone;
    lovecityCell.selectBtn.hidden = YES;
    if(indexPath.section == 0)
    {
        lovecityCell.NameLabel.text = selectLoveCity;
        lovecityCell.LineImage.hidden = NO;
    }else{
        selectCityArrayModel *model = [selectCityArrayModel objectFromDictionary:self.cellArray[indexPath.section - 1]];
        selectCityValueModel *cityModel = model.city[indexPath.row];
        lovecityCell.NameLabel.text = cityModel.name;
        lovecityCell.tag = [cityModel.city_id intValue];
        if(indexPath.row == model.city.count - 1)
        {
            lovecityCell.lineLift.constant = 0;
        }
        [self.MycellArray addObject:lovecityCell];
    }
    return lovecityCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int num = 0;
    for (int i = 0; i < self.MycellArray.count; i++)
    {
        LoveCityCell *cell = self.MycellArray[i];
        if(cell.selectBtn.hidden == NO)
        {
            num ++;
        }
    }
    if(indexPath.section > 0)
    {
        LoveCityCell *lovecityCell = (LoveCityCell *)[tableView cellForRowAtIndexPath:indexPath];
        if(num > 4 )
        {
            if(lovecityCell.selectBtn.hidden == YES)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您已经选择了5个城市！" message:@"" delegate:self cancelButtonTitle:@"确认" otherButtonTitles: nil];
                [alert show];
                
            }else{
                lovecityCell.selectBtn.hidden = YES;
            }
        }else{
            if(lovecityCell.selectBtn.hidden == NO)
            {
                lovecityCell.selectBtn.hidden = YES;
            }else{
                lovecityCell.selectBtn.hidden = NO;
            }
        }
    }
    self.NamelArray = [[NSMutableArray alloc]init];
    self.IDcellArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < self.MycellArray.count; i++)
    {
        LoveCityCell *cell = self.MycellArray[i];
        if(cell.selectBtn.hidden == NO)
        {
            [self.IDcellArray addObject:[NSString stringWithFormat:@"%li",(long)cell.tag]];
            [self.NamelArray addObject:cell.NameLabel.text];
        }
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
